package com.sudu.service.impl;

import com.sudu.controller.Code;
import com.sudu.dao.LikeDao;
import com.sudu.dao.TagDao;
import com.sudu.dao.UserDao;
import com.sudu.dao.VideoDao;
import com.sudu.domain.*;
import com.sudu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private TagDao tagDao ;
    @Autowired
    private VideoDao videoDao ;
    @Autowired
    private UserDao userDao ;
    @Autowired
    private LikeDao likeDao ;

    @Override
    public String fineById(int id) {
        return tagDao.fineById(id);
    }

    @Override
    public List<Tag> fineByAll() {
        return tagDao.fineByAll();
    }

    @Override
    public boolean saveVideo(Video video) {
        return videoDao.saveVideo(video) > 0;
    }

    @Override
    public List<Video> selectByUid(int uid) {
        return videoDao.selectByUid(uid);
    }

    @Override
    public Detail selectByVid(int vid) {
        return videoDao.selectByVid(vid);
    }

    @Override
    public List<Cover> selectByPage(int tagId, int page, int pageSize) {
        return videoDao.selectByPage(tagId , page , pageSize);
    }

    @Override
    public List<Cover> selectCoverByUid(int uid) {
        return videoDao.selectCoverByUid(uid);
    }

    @Override
    public List<Cover> selectCoverByTag(int tag , int size) {;

        return videoDao.selectCoverByTag(tag, size);
    }

    @Override
    public UserInfo selectUpInfoByVid(int vid) {
        int Uid = videoDao.selectUidByVid(vid) ;
        return userDao.getUserInfoById(Uid);
    }

    @Override
    public List<Cover> selectCoverByVid(int[] vid , int tagId , int page , int pageSize) {
        int len = 0 ;
        List<Cover> list = new ArrayList<>() ;
        Cover cover ;
        while (len < vid.length) {
            cover = videoDao.selectCoverByVid(vid[len++] , tagId ,page ,pageSize) ;
            if(cover != null) {
                list.add(cover) ;
            }
        }
        return list;
    }

    @Override
    public List<Cover> getCoverByUid(int uid, int tagId, int page, int pageSize) {
        return videoDao.getCoverByUid(uid, tagId, page, pageSize);
    }

    @Override
    public List<Tag> getMyCreateTag( int uid) {
        int [] arr = videoDao.getTagIdByUid(uid) ;
        Set set = new HashSet();
        for (int len=0 ; len<arr.length ; len++){
            set.add(arr[len]);
        }
        List<Tag> tags = new ArrayList<>() ;
        Iterator it = set.iterator();
        int index ;
        while (it.hasNext()) {
            index = (int) it.next();
            tags.add(new Tag(index , tagDao.fineById(index))) ;
        }
        return tags;
    }

    @Override
    public List<Tag> getMyFavoriteTag(int uid) {
        int [] vid = likeDao.selectVidByUid(uid) ;
        Set set = new HashSet();
        for (int len=0 ; len<vid.length ; len++){
            set.add(videoDao.getTagIdByVid(vid[len])) ;
        }
        List<Tag> tags = new ArrayList<>() ;
        Iterator it = set.iterator();
        int index ;
        while (it.hasNext()) {
            index = (int) it.next();
            tags.add(new Tag(index , tagDao.fineById(index))) ;
        }
        return tags;
    }

    @Override
    public Boolean deleteByVid(int vid) {
        return videoDao.deleteByVid(vid) > 0 ;
    }

    @Override
    public Boolean click(int vid) {
        return videoDao.click(vid) > 0;
    }

    @Override
    public Boolean comment(int vid) {
        return videoDao.comment(vid) > 0;
    }

    @Override
    public List<Cover> search(String content , int tagId , int page , int pageSize) {
        content = '%' + content + '%' ;
        return videoDao.search(content ,  tagId ,  page ,  pageSize);
    }

    @Override
    public List<Tag> searchTagByContent(String content) {
        content = '%' + content + '%' ;
        int [] vid = videoDao.searchVidByContent(content);
        Set set = new HashSet();
        for (int len=0 ; len<vid.length ; len++){
            set.add(videoDao.getTagIdByVid(vid[len])) ;
        }
        List<Tag> tags = new ArrayList<>() ;
        Iterator it = set.iterator();
        int index ;
        while (it.hasNext()) {
            index = (int) it.next();
            tags.add(new Tag(index , tagDao.fineById(index))) ;
        }
        return tags;
    }

    @Override
    public boolean delCom(int vid) {
        return videoDao.delCom(vid) > 0 ;
    }

}
